<template>
  <div class="cart">
    <h4>
      全部商品

      <p>用 token 值 来判断每一个用户的购物车</p>
    </h4>
    <div class="cart-main">
      <div class="cart-th">
        <div class="cart-th1">全部</div>
        <div class="cart-th2">商品</div>
        <div class="cart-th3">单价（元）</div>
        <div class="cart-th4">数量</div>
        <div class="cart-th5">小计（元）</div>
        <div class="cart-th6">操作</div>
      </div>
      <div class="cart-body">
        <ul
          class="cart-list"
          v-for="(a1, index) in cartsList"
          :key="a1.goodsID + index"
        >
          <li class="cart-list-con1">
            <input
              type="checkbox"
              :checked="a1.stateCheck != '-1'"
              name="chk_list"
              @change="updateChecked(a1._id, $event)"
            />
          </li>
          <li class="cart-list-con2">
            <img :src="a1.goodsImg" />
            <div class="a1-msg">
              {{ a1.title }}
            </div>
          </li>
          <li class="cart-list-con3">
            <div class="a1-txt">
              <span v-if="a1.goodsFiltering == null" style="color: red"
                >没有筛选属性功能--占位</span
              >
              <span v-for="a2 in a1.goodsFiltering" :key="a2.id">
                {{ a2.text }}
              </span>
            </div>
          </li>
          <li class="cart-list-con4">
            <span class="price">${{ a1.price }}</span>
          </li>
          <li class="cart-list-con5">
            <!-- 处理 在不发送请求的 情况下该改变商品数量 -->

            <a
              href="javascript:void(0)"
              class="mins"
              @click="changeCount('sub', a1.goodsCount, a1._id)"
            >
              -
            </a>
            <input
              autocomplete="off"
              type="text"
              class="itxt"
              v-model="a1.goodsCount"
              @input="
                a1.goodsCount = isNaN(a1.goodsCount)
                  ? a1.goodsCount.replace(/[^\d]/g, '')
                  : a1.goodsCount
              "
              @change="changeCount('input', $event, a1._id)"
            />

            <a
              href="javascript:void(0)"
              class="plus"
              @click="changeCount('add', a1.goodsCount, a1._id)"
              >+</a
            >
          </li>
          <li class="cart-list-con6">
            <span class="sum">${{ a1.price * a1.goodsCount }}</span>
          </li>
          <li class="cart-list-con7">
            <a
              href="javascript:;"
              class="sindelet"
              @click="DeleteGoods(a1._id)"
              style="color: red"
              >删除</a
            >
            <br />
            <a href="javascript:;">移到收藏</a>
          </li>
        </ul>
      </div>
    </div>
    <div class="cart-tool">
      <div class="select-all">
        <input
          class="chooseAll"
          type="checkbox"
          :checked="allChecked"
          @change="updateChecked('all', $event)"
        />
        <span>全选</span>
      </div>
      <div class="option">
        <a href="javascript:;" @click="ScreeningDelete" style="color: red"
          >删除选中的商品</a
        >
        <a href="javascript:;">移到我的关注</a>
        <a href="javascript:;">清除下柜商品</a>
      </div>
      <div class="money-box">
        <div class="chosed">已选择 <span>0</span>件商品</div>
        <div class="sumprice">
          <em>总价（不含运费）</em>
          <i class="summoney">${{ sumPrice }}</i>
        </div>
        <div class="sumbtn">
          <a class="sum-btn" href="###" target="_blank">结算</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Carts',
  data() {
    return {
      cartsInfo: {
        _id: '', // 数据库自动生成的唯一标识
        stateCheck: '', // 勾选产品的状态
        state: '', // 修改产品个数
        delete: '', // 删除单个商品
        ScreeningDelete: '', // 删除被选中的商品
        Who: '' // 判断是单选还是全选
      }
    }
  },
  mounted() {
    // 发送请求
    this.cartsData()
  },

  computed: {
    // 购物车中的数据
    cartsList() {
      return this.$store.state.carts.cartsList
    },
    // 计算购买产品的总价
    sumPrice() {
      let sum = 0
      //  this.cartsList.length > 0  防止数据还没加载 就执行
      if (this.cartsList.length > 0) {
        this.cartsList
          .filter((item) => item.stateCheck != '-1')
          .forEach((item) => {
            sum += item.goodsCount * item.price
          })
      }

      return sum
    },
    // 底部全选是否选中
    allChecked() {
      //  this.cartsList.length > 0  防止数据还没加载 就执行
      if (this.cartsList.length > 0) {
        return this.cartsList.every((item) => item.stateCheck == '1')
      }
    },
    // 筛选已选中的商品进行删除操作
    ScreeningChecked() {
      let ScreeningChecked = []
      if (this.cartsList.length > 0) {
        this.cartsList
          .filter((item) => item.stateCheck != '-1')
          .forEach((item) => {
            ScreeningChecked.push(item._id)
          })
      }
      return ScreeningChecked
    }
  },
  methods: {
    cartsData() {
      this.$store.dispatch('cartsList', this.cartsInfo)
    },
    // 添加和减少商品
    changeCount(val, e, ID) {
      switch (val) {
        case 'input':
          this.cartsInfo.state = { event: val, count: Number(e.target.value) }
          this.cartsInfo._id = ID
          break
        case 'add':
          this.cartsInfo.state = { event: val, count: e }
          this.cartsInfo._id = ID
          break
        case 'sub':
          this.cartsInfo.state = { event: val, count: e }
          this.cartsInfo._id = ID
          break
      }
      this.cartsInfo.delete = '' // 清空删除记录
      this.cartsInfo.ScreeningDelete = '' // 清空删除记录
      // 发送请求
      this.cartsData()
    },
    // 复选框更新商品状态
    updateChecked(_id, event) {
      if (_id == 'all') {
        console.log(event.target.checked)
        // 全选按钮
        if (event.target.checked == true) {
          this.cartsInfo.Who = 'all'
          this.cartsInfo.stateCheck = '1'
        } else {
          this.cartsInfo.Who = 'noAll'
          this.cartsInfo.stateCheck = '-1'
        }
      } else {
        // 单个商品按钮
        this.cartsInfo._id = _id
        this.cartsInfo.Who = 'single'
        if (event.target.checked == true) {
          this.cartsInfo.stateCheck = '1'
        } else {
          this.cartsInfo.stateCheck = '-1'
        }
      }
      this.cartsInfo.delete = '' // 清空删除记录
      this.cartsInfo.ScreeningDelete = '' // 清空删除记录
      this.cartsData() // 发送请求
      this.cartsData() // 同步数据
    },
    // 删除单个商品
    DeleteGoods(_id) {
      this.cartsInfo._id = _id
      this.cartsInfo.delete = 'DeleteGoods'
      this.cartsData() // 发送请求
    },
    // 删除被选中的商品
    ScreeningDelete() {
      this.cartsInfo.ScreeningDelete = this.ScreeningChecked
      this.cartsData() // 发送请求
    }
  }
}
</script>

<style lang="less" scoped>
.cart {
  width: 1200px;
  margin: 0 auto;

  h4 {
    margin: 9px 0;
    font-size: 14px;
    line-height: 21px;
  }

  .cart-main {
    .cart-th {
      background: #f5f5f5;
      border: 1px solid #ddd;
      padding: 10px;
      overflow: hidden;

      & > div {
        float: left;
      }

      .cart-th1 {
        width: 25%;

        input {
          vertical-align: middle;
        }

        span {
          vertical-align: middle;
        }
      }

      .cart-th2 {
        width: 25%;
      }

      .cart-th3,
      .cart-th4,
      .cart-th5,
      .cart-th6 {
        width: 12.5%;
      }
    }

    .cart-body {
      margin: 15px 0;
      border: 1px solid #ddd;

      .cart-list {
        padding: 10px;
        border-bottom: 1px solid #ddd;
        overflow: hidden;

        & > li {
          float: left;
        }

        .cart-list-con1 {
          width: 4.1667%;
        }

        .cart-list-con2 {
          width: 25%;

          img {
            width: 82px;
            height: 82px;
            float: left;
          }

          .a1-msg {
            float: left;
            width: 150px;
            margin: 0 10px;
            line-height: 18px;
          }
        }

        .cart-list-con3 {
          width: 20.8333%;

          .a1-txt {
            text-align: center;
          }
        }

        .cart-list-con4 {
          width: 12.5%;
        }

        .cart-list-con5 {
          width: 12.5%;

          .mins {
            border: 1px solid #ddd;
            border-right: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
          }

          input {
            border: 1px solid #ddd;
            width: 40px;
            height: 33px;
            float: left;
            text-align: center;
            font-size: 14px;
            outline: none;
          }

          .plus {
            border: 1px solid #ddd;
            border-left: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
          }
        }

        .cart-list-con6 {
          width: 12.5%;

          .sum {
            font-size: 16px;
          }
        }

        .cart-list-con7 {
          width: 12.5%;

          a {
            color: #666;
          }
        }
      }
    }
  }

  .cart-tool {
    overflow: hidden;
    border: 1px solid #ddd;

    .select-all {
      padding: 10px;
      overflow: hidden;
      float: left;

      span {
        vertical-align: middle;
      }

      input {
        vertical-align: middle;
      }
    }

    .option {
      padding: 10px;
      overflow: hidden;
      float: left;

      a {
        float: left;
        padding: 0 10px;
        color: #666;
      }
    }

    .money-box {
      float: right;

      .chosed {
        line-height: 26px;
        float: left;
        padding: 0 10px;
      }

      .sumprice {
        width: 200px;
        line-height: 22px;
        float: left;
        padding: 0 10px;

        .summoney {
          color: #c81623;
          font-size: 16px;
        }
      }

      .sumbtn {
        float: right;

        a {
          display: block;
          position: relative;
          width: 96px;
          height: 52px;
          line-height: 52px;
          color: #fff;
          text-align: center;
          font-size: 18px;
          font-family: 'Microsoft YaHei';
          background: #e1251b;
          overflow: hidden;

          &:hover {
            color: #000 !important;
          }
        }
      }
    }
  }
}
</style>
